package com.wdh.kjs.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.Adapter.MF_invest_Adapter;
import com.wdh.kjs.Adapter.MF_invest_Adapter2;
import com.wdh.kjs.R;
import com.wdh.kjs.Adapter.MF_invest_Adapter;
import com.wdh.kjs.Adapter.ListViewForScrollView;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel_my;
import com.wdh.kjs.model.JsonModel_my_MF;
import com.wdh.kjs.util.DateUtils;
import com.wdh.kjs.util.ToastUtils;
import com.wdh.kjs.widget.MyTabView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenqinhuang on 2016/4/3.
 */
public class Money_flowing_Fragment extends Fragment {

    private PullToRefreshListView listView;
    private MF_invest_Adapter MF_invest_Adapter;
    private MF_invest_Adapter2 mf_invest_Adaper2;
    public int QUERY_TYPE=0;
    private JsonModel_my_MF jsonModel_my_mf;
    private String partyId;
    private TextView tv_fragment_moneyflowing=null;
    private int start=0;
    private int length=2;
    private String textMesssge="暂无内容";

    private boolean mLoading=true;
    private List<HashMap<String, String>> datas=new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_moneyflowing_2, container, false);
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("logininfo", Context.MODE_PRIVATE);
        partyId = sharedPreferences.getString("loginToken", "");
        initView(view);
        loadData();
        return view;
    }

    private void initView(View view) {
        listView=(PullToRefreshListView)view.findViewById(R.id.fragment_moneyFlowing_listview);
        tv_fragment_moneyflowing=(TextView) view.findViewById(R.id.tv_fragment_moneyflowing);
    }

    private void setPulllistViewListener() {
        listView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            // 下拉Pulling Down
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 下拉的时候数据重置
            }

            // 上拉Pulling Up
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 上拉的时候添加选项

                String label = DateUtils.formatDateTime(getActivity().getApplicationContext(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                new GetDataTask().execute();
            }

        });
    }

    private void loadData()
    {
        switch (QUERY_TYPE)
        {
            case 0:
                RequestParams params = new RequestParams();
                params.addBodyParameter("partyId", partyId);
                params.addBodyParameter("start", start+"");
                params.addBodyParameter("length", length+"");
                HttpUtils http = new HttpUtils();
                http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_MF_INVEST, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Gson gson = new Gson();
                        jsonModel_my_mf = gson.fromJson(responseInfo.result, JsonModel_my_MF.class);
                        if (jsonModel_my_mf.success) {
                            MF_invest_Adapter=new MF_invest_Adapter(getContext(),initListViewData());
                            listView.setAdapter(MF_invest_Adapter);
                            setPulllistViewListener();
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        ToastUtils.showShort(getActivity(), "网络异常！");
                    }
                });

                break;
            case 1:
                RequestParams params_1 = new RequestParams();
                params_1.addBodyParameter("partyId", partyId);
                params_1.addBodyParameter("start", start+"");
                params_1.addBodyParameter("length", length+"");

                HttpUtils http_1 = new HttpUtils();
                http_1.send(HttpRequest.HttpMethod.POST, Httpurl.URL_MF_FINANCE, params_1, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Gson gson = new Gson();
                        jsonModel_my_mf = gson.fromJson(responseInfo.result, JsonModel_my_MF.class);
                        if (jsonModel_my_mf.success) {
                           // MF_invest_Adapter=new MF_invest_Adapter(getContext(),initListViewData());
                            mf_invest_Adaper2=new MF_invest_Adapter2(getContext(),initListViewData());
                           // listView.setAdapter(MF_invest_Adapter);
                            listView.setAdapter(mf_invest_Adaper2);
                            setPulllistViewListener();
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        ToastUtils.showShort(getActivity(), "网络异常！");
                    }
                });
                break;
            case 2:
                RequestParams params_2 = new RequestParams();
                params_2.addBodyParameter("partyId", partyId);
                params_2.addBodyParameter("start", start+"");
                params_2.addBodyParameter("length", length+"");

                HttpUtils http_2 = new HttpUtils();
                http_2.send(HttpRequest.HttpMethod.POST, Httpurl.URL_MF_RW, params_2, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Gson gson = new Gson();
                        jsonModel_my_mf = gson.fromJson(responseInfo.result, JsonModel_my_MF.class);
                        if (jsonModel_my_mf.success) {
                            MF_invest_Adapter=new MF_invest_Adapter(getContext(),initListViewData());
                            listView.setAdapter(MF_invest_Adapter);
                            setPulllistViewListener();
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        ToastUtils.showShort(getActivity(), "网络异常！");
                    }
                });
                break;

        }

    }
    private List<HashMap<String, String>> initListViewData()
    {
        datas=new ArrayList<>();
        switch (QUERY_TYPE)
        {
            case 0: {
                int i = 0;
                for (; i < jsonModel_my_mf.getRecordsFiltered(); i++) {
                    HashMap<String, String> hashMap = new HashMap<>();
                    switch (jsonModel_my_mf.getAaData().get(i).getType().toString()) {
                        case "chongzhi":
                            hashMap.put("moneyFlowing_abstract", "充值");
                            break;
                        case "tixianchenggong":
                            hashMap.put("moneyFlowing_abstract", "提现成功");
                            break;
                        case "tixiandongjie":
                            hashMap.put("moneyFlowing_abstract", "提现冻结");
                            break;
                        case "zqrzbenjinfanhuan":
                            hashMap.put("moneyFlowing_abstract", "本金返还");
                            break;
                        case "zqrzchoukuanhuabo":
                            hashMap.put("moneyFlowing_abstract", "筹款划拨");
                            break;
                        case "zqrzchenggong":
                            hashMap.put("moneyFlowing_abstract", "融资成功");
                            break;
                        case "zqtzchenggong":
                            hashMap.put("moneyFlowing_abstract", "投资成功");
                            break;
                        case "zqtzdongjie":
                            hashMap.put("moneyFlowing_abstract", "投资冻结");
                            break;
                        case "zqtzbenjinshouhui":
                            hashMap.put("moneyFlowing_abstract", "本金收回");
                            break;
                        case "zqtztuihui":
                            hashMap.put("moneyFlowing_abstract", "募集终止退回");
                            break;
                        case "zqrzzhifulixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "支付利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        case "zqtzshouhuilixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "收回利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        default:
                            break;
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("yuezhifu")) {
                        hashMap.put("moneyFlowing_payMode", "余额支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("wangyinzhifu")) {
                        hashMap.put("moneyFlowing_payMode", "网银支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("xianxiazhifu")) {
                        hashMap.put("moneyFlowing_payMode", "线下支付");
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 0) {
                        hashMap.put("moneyFlowing_repayType", "到期一次性还本付息");
                    } else if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 1) {
                        hashMap.put("moneyFlowing_repayType", "按月付息到期还本");
                    }


                  //  int annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + "")).intValue();
                    DecimalFormat df = new DecimalFormat("0.00");
                    BigDecimal annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + ""));
                    String  newrate1=df.format(annualRate);


                    hashMap.put("moneyFlowing_name", jsonModel_my_mf.getAaData().get(i).getItemName().toString());
                   // hashMap.put("moneyFlowing_annualRate", annualRate + "%");
                    hashMap.put("moneyFlowing_annualRate", newrate1 + "%");
                    hashMap.put("moneyFlowing_projectTime", jsonModel_my_mf.getAaData().get(i).getInterestDays() + "天");
                    hashMap.put("moneyFlowing_investMoney", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAmount().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_startTime", jsonModel_my_mf.getAaData().get(i).getDate().toString().substring(0, 10));
                    hashMap.put("moneyFlowing_availableBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAvailableBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_blockedBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBlockedBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_cash", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBondInvest().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_earning", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondInvestIncome().toString()).setScale(2) + "元");
                    datas.add(hashMap);
                }
                if(i==0) {
                    mLoading = false;
                    tv_fragment_moneyflowing.setVisibility(View.VISIBLE);
                }
                else if(i<length) {
                    mLoading = false;
                }
                break;
            }
            case 1: {
                int i = 0;
                for (; i < jsonModel_my_mf.getRecordsFiltered(); i++) {
                    HashMap<String, String> hashMap = new HashMap<>();
                    switch (jsonModel_my_mf.getAaData().get(i).getGdItemType().toString()) {
                        case "0":
                            hashMap.put("moneyFlowing_type", "政信资产贷");
                            break;
                        case "1":
                            hashMap.put("moneyFlowing_type", "中小企业担保贷");
                            break;
                        case "2":
                            hashMap.put("moneyFlowing_type", "物权抵押贷");
                            break;
                        case "3":
                            hashMap.put("moneyFlowing_type", "普惠三农贷");
                            break;
                        case "4":
                            hashMap.put("moneyFlowing_type", "消费金融贷");
                            break;
                        default:
                            break;
                    }

                    switch (jsonModel_my_mf.getAaData().get(i).getType().toString()) {
                        case "chongzhi":
                            hashMap.put("moneyFlowing_abstract", "充值");
                            break;
                        case "tixianchenggong":
                            hashMap.put("moneyFlowing_abstract", "提现成功");
                            break;
                        case "tixiandongjie":
                            hashMap.put("moneyFlowing_abstract", "提现冻结");
                            break;
                        case "zqrzbenjinfanhuan":
                            hashMap.put("moneyFlowing_abstract", "本金返还");
                            break;
                        case "zqrzchoukuanhuabo":
                            hashMap.put("moneyFlowing_abstract", "筹款划拨");
                            break;
                        case "zqrzchenggong":
                            hashMap.put("moneyFlowing_abstract", "融资成功");
                            break;
                        case "zqtzchenggong":
                            hashMap.put("moneyFlowing_abstract", "投资成功");
                            break;
                        case "zqtzdongjie":
                            hashMap.put("moneyFlowing_abstract", "投资冻结");
                            break;
                        case "zqtzbenjinshouhui":
                            hashMap.put("moneyFlowing_abstract", "本金收回");
                            break;
                        case "zqtztuihui":
                            hashMap.put("moneyFlowing_abstract", "募集终止退回");
                            break;
                        case "zqrzzhifulixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "支付利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        case "zqtzshouhuilixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "收回利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        default:
                            break;
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("yuezhifu")) {
                        hashMap.put("moneyFlowing_payMode", "余额支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("wangyinzhifu")) {
                        hashMap.put("moneyFlowing_payMode", "网银支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("xianxiazhifu")) {
                        hashMap.put("moneyFlowing_payMode", "线下支付");
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 0) {
                        hashMap.put("moneyFlowing_repayType", "到期一次性还本付息");
                    } else if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 1) {
                        hashMap.put("moneyFlowing_repayType", "按月付息到期还本");
                    }
                    DecimalFormat df = new DecimalFormat("0.00");
                    BigDecimal annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + ""));
                    String  newrate1=df.format(annualRate);
                    hashMap.put("moneyFlowing_name", jsonModel_my_mf.getAaData().get(i).getItemName().toString());
                    hashMap.put("moneyFlowing_annualRate", newrate1 + "%");
                    hashMap.put("moneyFlowing_projectTime", jsonModel_my_mf.getAaData().get(i).getInterestDays() + "天");
                    hashMap.put("moneyFlowing_investMoney", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAmount().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_startTime", jsonModel_my_mf.getAaData().get(i).getDate().toString().substring(0, 10));
                   // hashMap.put("moneyFlowing_availableBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAvailableBalance().toString()).setScale(2) + "元");
                   // hashMap.put("moneyFlowing_blockedBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBlockedBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_blockedBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondRaiseInterestStill().toString()).setScale(2) + "元");
                  //  hashMap.put("moneyFlowing_cash", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBondInvest().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_cash", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondRaisePrincipleStill().toString()).setScale(2) + "元");
                    // hashMap.put("moneyFlowing_earning", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondInvestIncome().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_earning", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondRaiseInterestRepay().toString()).setScale(2) + "元");
                    datas.add(hashMap);
                }
                if(i==0) {
                    mLoading = false;
                    tv_fragment_moneyflowing.setVisibility(View.VISIBLE);
                }
                else if(i<length) {
                    mLoading = false;
                }
                break;
            }
            case 2: {
                int i = 0;
                for (; i < jsonModel_my_mf.getRecordsFiltered(); i++) {
                    HashMap<String, String> hashMap = new HashMap<>();
                    switch (jsonModel_my_mf.getAaData().get(i).getGdItemType().toString()) {
                        case "0":
                            hashMap.put("moneyFlowing_type", "政信资产贷");
                            break;
                        case "1":
                            hashMap.put("moneyFlowing_type", "中小企业担保贷");
                            break;
                        case "2":
                            hashMap.put("moneyFlowing_type", "物权抵押贷");
                            break;
                        case "3":
                            hashMap.put("moneyFlowing_type", "普惠三农贷");
                            break;
                        case "4":
                            hashMap.put("moneyFlowing_type", "消费金融贷");
                            break;
                        default:
                            break;
                    }

                    switch (jsonModel_my_mf.getAaData().get(i).getType().toString()) {
                        case "chongzhi":
                            hashMap.put("moneyFlowing_abstract", "充值");
                            break;
                        case "tixianchenggong":
                            hashMap.put("moneyFlowing_abstract", "提现成功");
                            break;
                        case "tixiandongjie":
                            hashMap.put("moneyFlowing_abstract", "提现冻结");
                            break;
                        case "zqrzbenjinfanhuan":
                            hashMap.put("moneyFlowing_abstract", "本金返还");
                            break;
                        case "zqrzchoukuanhuabo":
                            hashMap.put("moneyFlowing_abstract", "筹款划拨");
                            break;
                        case "zqrzchenggong":
                            hashMap.put("moneyFlowing_abstract", "融资成功");
                            break;
                        case "zqtzchenggong":
                            hashMap.put("moneyFlowing_abstract", "投资成功");
                            break;
                        case "zqtzdongjie":
                            hashMap.put("moneyFlowing_abstract", "投资冻结");
                            break;
                        case "zqtzbenjinshouhui":
                            hashMap.put("moneyFlowing_abstract", "本金收回");
                            break;
                        case "zqtztuihui":
                            hashMap.put("moneyFlowing_abstract", "募集终止退回");
                            break;
                        case "zqrzzhifulixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "支付利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        case "zqtzshouhuilixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "收回利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        default:
                            break;
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("yuezhifu")) {
                        hashMap.put("moneyFlowing_payMode", "余额支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("wangyinzhifu")) {
                        hashMap.put("moneyFlowing_payMode", "网银支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("xianxiazhifu")) {
                        hashMap.put("moneyFlowing_payMode", "线下支付");
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 0) {
                        hashMap.put("moneyFlowing_repayType", "到期一次性还本付息");
                    } else if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 1) {
                        hashMap.put("moneyFlowing_repayType", "按月付息到期还本");
                    }

                  //  int annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + "")).intValue();
                    DecimalFormat df = new DecimalFormat("0.00");
                    BigDecimal annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + ""));
                    String  newrate1=df.format(annualRate);
                    hashMap.put("moneyFlowing_name", jsonModel_my_mf.getAaData().get(i).getItemName().toString());
                   // hashMap.put("moneyFlowing_annualRate", annualRate + "%");
                    hashMap.put("moneyFlowing_annualRate", newrate1 + "%");
                    hashMap.put("moneyFlowing_projectTime", jsonModel_my_mf.getAaData().get(i).getInterestDays() + "天");
                    hashMap.put("moneyFlowing_investMoney", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAmount().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_startTime", jsonModel_my_mf.getAaData().get(i).getDate().toString().substring(0, 10));
                    hashMap.put("moneyFlowing_availableBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAvailableBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_blockedBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBlockedBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_cash", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBondInvest().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_earning", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondInvestIncome().toString()).setScale(2) + "元");
                    datas.add(hashMap);
                }
                if(i==0) {
                    mLoading = false;
                    tv_fragment_moneyflowing.setVisibility(View.VISIBLE);
                }
                else if(i<length) {
                    mLoading = false;
                }
                break;
            }
        }

        return datas;
    }


    private class GetDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (mLoading) {
                start=start+length;
                try {
                    getMoreList();
                } catch (Exception e) {
                    start=start-length;
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (mLoading == false) {
                ToastUtils.showShort(getActivity(), "已无更多数据！");
            }
            listView.onRefreshComplete();
            super.onPostExecute(aVoid);
        }
    }

    private  void getMoreList() {
        switch (QUERY_TYPE)
        {
            case 0:
                RequestParams params = new RequestParams();
                params.addBodyParameter("partyId", partyId);
                params.addBodyParameter("start", start+"");
                params.addBodyParameter("length", length+"");

                HttpUtils http = new HttpUtils();
                http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_MF_INVEST, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        try {
                            Gson gson = new Gson();
                            jsonModel_my_mf = gson.fromJson(responseInfo.result, JsonModel_my_MF.class);
                            if (jsonModel_my_mf.success) {
                                getMoreListViewDate();
                            }else{
                                mLoading=false;
                                start=start-length;
                            }
                        }catch (Exception e){
                            start=start-length;
                            mLoading=false;
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        ToastUtils.showShort(getActivity(), "网络异常！");
                    }
                });

                break;
            case 1:
                RequestParams params_1 = new RequestParams();
                params_1.addBodyParameter("partyId", partyId);
                params_1.addBodyParameter("start", start+"");
                params_1.addBodyParameter("length", length+"");

                HttpUtils http_1 = new HttpUtils();
                http_1.send(HttpRequest.HttpMethod.POST, Httpurl.URL_MF_FINANCE, params_1, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        try {
                            Gson gson = new Gson();
                            jsonModel_my_mf = gson.fromJson(responseInfo.result, JsonModel_my_MF.class);
                            if (jsonModel_my_mf.success) {
                                getMoreListViewDate();
                            }else{
                                mLoading=false;
                                start=start-length;
                            }
                        }catch (Exception e)
                        {
                            mLoading=false;
                            start=start-length;
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        ToastUtils.showShort(getActivity(), "网络异常！");
                    }
                });
                break;
            case 2:
                RequestParams params_2 = new RequestParams();
                params_2.addBodyParameter("partyId", partyId);
                params_2.addBodyParameter("start", start+"");
                params_2.addBodyParameter("length", length+"");

                HttpUtils http_2 = new HttpUtils();
                http_2.send(HttpRequest.HttpMethod.POST, Httpurl.URL_MF_RW, params_2, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        try{
                            Gson gson = new Gson();
                            jsonModel_my_mf = gson.fromJson(responseInfo.result, JsonModel_my_MF.class);
                            if (jsonModel_my_mf.success) {
                                getMoreListViewDate();
                            }else{
                                mLoading=false;
                                start=start-length;
                            }
                        }
                        catch (Exception e){
                            mLoading=false;
                            start=start-length;
                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        ToastUtils.showShort(getActivity(), "网络异常！");
                    }
                });
                break;
        }
    }


    private void getMoreListViewDate() {
        switch (QUERY_TYPE)
        {
            case 0: {
                int i = 0;
                for (; i < jsonModel_my_mf.getRecordsFiltered(); i++) {
                    HashMap<String, String> hashMap = new HashMap<>();
                    switch (jsonModel_my_mf.getAaData().get(i).getType().toString()) {
                        case "chongzhi":
                            hashMap.put("moneyFlowing_abstract", "充值");
                            break;
                        case "tixianchenggong":
                            hashMap.put("moneyFlowing_abstract", "提现成功");
                            break;
                        case "tixiandongjie":
                            hashMap.put("moneyFlowing_abstract", "提现冻结");
                            break;
                        case "zqrzbenjinfanhuan":
                            hashMap.put("moneyFlowing_abstract", "本金返还");
                            break;
                        case "zqrzchoukuanhuabo":
                            hashMap.put("moneyFlowing_abstract", "筹款划拨");
                            break;
                        case "zqrzchenggong":
                            hashMap.put("moneyFlowing_abstract", "融资成功");
                            break;
                        case "zqtzchenggong":
                            hashMap.put("moneyFlowing_abstract", "投资成功");
                            break;
                        case "zqtzdongjie":
                            hashMap.put("moneyFlowing_abstract", "投资冻结");
                            break;
                        case "zqtzbenjinshouhui":
                            hashMap.put("moneyFlowing_abstract", "本金收回");
                            break;
                        case "zqtztuihui":
                            hashMap.put("moneyFlowing_abstract", "募集终止退回");
                            break;
                        case "zqrzzhifulixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "支付利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        case "zqtzshouhuilixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "收回利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        default:
                            break;
                    }
                    if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("yuezhifu")) {
                        hashMap.put("moneyFlowing_payMode", "余额支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("wangyinzhifu")) {
                        hashMap.put("moneyFlowing_payMode", "网银支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("xianxiazhifu")) {
                        hashMap.put("moneyFlowing_payMode", "线下支付");
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 0) {
                        hashMap.put("moneyFlowing_repayType", "到期一次性还本付息");
                    } else if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 1) {
                        hashMap.put("moneyFlowing_repayType", "按月付息到期还本");
                    }

                //    int annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + "")).intValue();
                    DecimalFormat df = new DecimalFormat("0.00");
                    BigDecimal annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + ""));
                    String  newrate1=df.format(annualRate);
                    hashMap.put("moneyFlowing_name", jsonModel_my_mf.getAaData().get(i).getItemName().toString());
                   // hashMap.put("moneyFlowing_annualRate", annualRate + "%");
                    hashMap.put("moneyFlowing_annualRate", newrate1 + "%");
                    hashMap.put("moneyFlowing_projectTime", jsonModel_my_mf.getAaData().get(i).getInterestDays() + "天");
                    hashMap.put("moneyFlowing_investMoney", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAmount().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_startTime", jsonModel_my_mf.getAaData().get(i).getDate().toString().substring(0, 10));
                    hashMap.put("moneyFlowing_availableBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAvailableBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_blockedBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBlockedBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_cash", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBondInvest().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_earning", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondInvestIncome().toString()).setScale(2) + "元");
                    datas.add(hashMap);
                }
                if (i < length) {
                    mLoading = false;
                }
                if (i > 0) {
                    MF_invest_Adapter.notifyDataSetChanged();
                }
                break;
            }
            case 1: {
                int i = 0;
                for (; i < jsonModel_my_mf.getRecordsFiltered(); i++) {
                    HashMap<String, String> hashMap = new HashMap<>();
                    switch (jsonModel_my_mf.getAaData().get(i).getGdItemType().toString()) {
                        case "0":
                            hashMap.put("moneyFlowing_type", "政信资产贷");
                            break;
                        case "1":
                            hashMap.put("moneyFlowing_type", "中小企业担保贷");
                            break;
                        case "2":
                            hashMap.put("moneyFlowing_type", "物权抵押贷");
                            break;
                        case "3":
                            hashMap.put("moneyFlowing_type", "普惠三农贷");
                            break;
                        case "4":
                            hashMap.put("moneyFlowing_type", "消费金融贷");
                            break;
                        default:
                            break;
                    }

                    switch (jsonModel_my_mf.getAaData().get(i).getType().toString()) {
                        case "chongzhi":
                            hashMap.put("moneyFlowing_abstract", "充值");
                            break;
                        case "tixianchenggong":
                            hashMap.put("moneyFlowing_abstract", "提现成功");
                            break;
                        case "tixiandongjie":
                            hashMap.put("moneyFlowing_abstract", "提现冻结");
                            break;
                        case "zqrzbenjinfanhuan":
                            hashMap.put("moneyFlowing_abstract", "本金返还");
                            break;
                        case "zqrzchoukuanhuabo":
                            hashMap.put("moneyFlowing_abstract", "筹款划拨");
                            break;
                        case "zqrzchenggong":
                            hashMap.put("moneyFlowing_abstract", "融资成功");
                            break;
                        case "zqtzchenggong":
                            hashMap.put("moneyFlowing_abstract", "投资成功");
                            break;
                        case "zqtzdongjie":
                            hashMap.put("moneyFlowing_abstract", "投资冻结");
                            break;
                        case "zqtzbenjinshouhui":
                            hashMap.put("moneyFlowing_abstract", "本金收回");
                            break;
                        case "zqtztuihui":
                            hashMap.put("moneyFlowing_abstract", "募集终止退回");
                            break;
                        case "zqrzzhifulixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "支付利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        case "zqtzshouhuilixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "收回利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        default:
                            break;
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("yuezhifu")) {
                        hashMap.put("moneyFlowing_payMode", "余额支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("wangyinzhifu")) {
                        hashMap.put("moneyFlowing_payMode", "网银支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("xianxiazhifu")) {
                        hashMap.put("moneyFlowing_payMode", "线下支付");
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 0) {
                        hashMap.put("moneyFlowing_repayType", "到期一次性还本付息");
                    } else if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 1) {
                        hashMap.put("moneyFlowing_repayType", "按月付息到期还本");
                    }
                //    int annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + "")).intValue();
                    DecimalFormat df = new DecimalFormat("0.00");
                    BigDecimal annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + ""));
                    String  newrate1=df.format(annualRate);
                    hashMap.put("moneyFlowing_name", jsonModel_my_mf.getAaData().get(i).getItemName().toString());
                  //  hashMap.put("moneyFlowing_annualRate", annualRate + "%");
                    hashMap.put("moneyFlowing_annualRate", newrate1 + "%");
                    hashMap.put("moneyFlowing_projectTime", jsonModel_my_mf.getAaData().get(i).getInterestDays() + "天");
                    hashMap.put("moneyFlowing_investMoney", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAmount().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_startTime", jsonModel_my_mf.getAaData().get(i).getDate().toString().substring(0, 10));
                  //  hashMap.put("moneyFlowing_availableBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAvailableBalance().toString()).setScale(2) + "元");
                  //  hashMap.put("moneyFlowing_blockedBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBlockedBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_blockedBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondRaiseInterestStill()).setScale(2) + "元");
                    //hashMap.put("moneyFlowing_cash", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBondInvest().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_cash", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondRaisePrincipleStill().toString()).setScale(2) + "元");
                  //  hashMap.put("moneyFlowing_earning", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondInvestIncome().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_earning", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondRaiseInterestRepay()).setScale(2) + "元");
                    datas.add(hashMap);
                }
                if (i < length) {
                    mLoading = false;
                }
                if (i > 0) {
                  //  MF_invest_Adapter.notifyDataSetChanged();
                    mf_invest_Adaper2.notifyDataSetChanged();
                }
                break;
            }
            case 2: {
                int i = 0;
                for (; i < jsonModel_my_mf.getRecordsFiltered(); i++) {
                    HashMap<String, String> hashMap = new HashMap<>();
                    switch (jsonModel_my_mf.getAaData().get(i).getGdItemType().toString()) {
                        case "0":
                            hashMap.put("moneyFlowing_type", "政信资产贷");
                            break;
                        case "1":
                            hashMap.put("moneyFlowing_type", "中小企业担保贷");
                            break;
                        case "2":
                            hashMap.put("moneyFlowing_type", "物权抵押贷");
                            break;
                        case "3":
                            hashMap.put("moneyFlowing_type", "普惠三农贷");
                            break;
                        case "4":
                            hashMap.put("moneyFlowing_type", "消费金融贷");
                            break;
                        default:
                            break;
                    }
                    switch (jsonModel_my_mf.getAaData().get(i).getType().toString()) {
                        case "chongzhi":
                            hashMap.put("moneyFlowing_abstract", "充值");
                            break;
                        case "tixianchenggong":
                            hashMap.put("moneyFlowing_abstract", "提现成功");
                            break;
                        case "tixiandongjie":
                            hashMap.put("moneyFlowing_abstract", "提现冻结");
                            break;
                        case "zqrzbenjinfanhuan":
                            hashMap.put("moneyFlowing_abstract", "本金返还");
                            break;
                        case "zqrzchoukuanhuabo":
                            hashMap.put("moneyFlowing_abstract", "筹款划拨");
                            break;
                        case "zqrzchenggong":
                            hashMap.put("moneyFlowing_abstract", "融资成功");
                            break;
                        case "zqtzchenggong":
                            hashMap.put("moneyFlowing_abstract", "投资成功");
                            break;
                        case "zqtzdongjie":
                            hashMap.put("moneyFlowing_abstract", "投资冻结");
                            break;
                        case "zqtzbenjinshouhui":
                            hashMap.put("moneyFlowing_abstract", "本金收回");
                            break;
                        case "zqtztuihui":
                            hashMap.put("moneyFlowing_abstract", "募集终止退回");
                            break;
                        case "zqrzzhifulixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "支付利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        case "zqtzshouhuilixi":
                            if (!jsonModel_my_mf.getAaData().get(i).getNote().toString().isEmpty() && !jsonModel_my_mf.getAaData().get(i).getNote().toString().equals("undefined"))
                                hashMap.put("moneyFlowing_abstract", "收回利息-" + jsonModel_my_mf.getAaData().get(i).getNote().toString());
                            break;
                        default:
                            break;
                    }
                    if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("yuezhifu")) {
                        hashMap.put("moneyFlowing_payMode", "余额支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("wangyinzhifu")) {
                        hashMap.put("moneyFlowing_payMode", "网银支付");
                    } else if (jsonModel_my_mf.getAaData().get(i).getOrderPayModeName().toString().equals("xianxiazhifu")) {
                        hashMap.put("moneyFlowing_payMode", "线下支付");
                    }

                    if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 0) {
                        hashMap.put("moneyFlowing_repayType", "到期一次性还本付息");
                    } else if (jsonModel_my_mf.getAaData().get(i).getGdBenefitMode() == 1) {
                        hashMap.put("moneyFlowing_repayType", "按月付息到期还本");
                    }
                   // int annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + "")).intValue();
                    DecimalFormat df = new DecimalFormat("0.00");
                    BigDecimal annualRate = new BigDecimal(jsonModel_my_mf.getAaData().get(i).getGdAnnualizedBenefitRate().toString()).multiply(new BigDecimal(100 + ""));
                    String  newrate1=df.format(annualRate);
                    hashMap.put("moneyFlowing_name", jsonModel_my_mf.getAaData().get(i).getItemName().toString());
                  //  hashMap.put("moneyFlowing_annualRate", annualRate + "%");
                    hashMap.put("moneyFlowing_annualRate", newrate1 + "%");
                    hashMap.put("moneyFlowing_projectTime", jsonModel_my_mf.getAaData().get(i).getInterestDays() + "天");
                    hashMap.put("moneyFlowing_investMoney", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAmount().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_startTime", jsonModel_my_mf.getAaData().get(i).getDate().toString().substring(0, 10));
                    hashMap.put("moneyFlowing_availableBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getAvailableBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_blockedBalance", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBlockedBalance().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_cash", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getBondInvest().toString()).setScale(2) + "元");
                    hashMap.put("moneyFlowing_earning", "￥" + new BigDecimal(jsonModel_my_mf.getAaData().get(i).getTotalBondInvestIncome().toString()).setScale(2) + "元");
                    datas.add(hashMap);
                }
                if (i < length) {
                    mLoading = false;
                }
                if (i > 0) {
                    MF_invest_Adapter.notifyDataSetChanged();
                }
                break;
            }
        }
    }
}
